home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_006 / microemacs / file.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  13KB  |  360 lines

  1. /*
  2.  * The routines in this file
  3.  * handle the reading and writing of
  4.  * disk files. All of details about the
  5.  * reading and writing of the disk are
  6.  * in "fileio.c".
  7.  */
  8. #include        <stdio.h>
  9. #include        "ed.h"
  10.  
  11. /*
  12.  * Read a file into the current
  13.  * buffer. This is really easy; all you do it
  14.  * find the name of the file, and call the standard
  15.  * "read a file into the current buffer" code.
  16.  * Bound to "C-X C-R".
  17.  */
  18. fileread(f, n)
  19. {
  20.         register int    s;
  21.         char            fname[NFILEN];
  22.  
  23.         if ((s=mlreply("Read file: ", fname, NFILEN)) != TRUE)
  24.                 return (s);
  25.         return (readin(fname));
  26. }
  27.  
  28. /*
  29.  * Select a file for editing.
  30.  * Look around to see if you can find the
  31.  * fine in another buffer; if you can find it
  32.  * just switch to the buffer. If you cannot find
  33.  * the file, create a new buffer, read in the
  34.  * text, and switch to the new buffer.
  35.  * Bound to C-X C-V.
  36.  */
  37. filevisit(f, n)
  38. {
  39.         register BUFFER *bp;
  40.         register WINDOW *wp;
  41.         register LINE   *lp;
  42.         register int    i;
  43.         register int    s;
  44.         char            bname[NBUFN];
  45.         char            fname[NFILEN];
  46.  
  47.         if ((s=mlreply("Visit file: ", fname, NFILEN)) != TRUE)
  48.                 return (s);
  49.         for (bp=bheadp; bp!=NULL; bp=bp->b_bufp) {
  50.                 if ((bp->b_flag&BFTEMP)==0 && strcmp(bp->b_fname, fname)==0) {
  51.                         if (--curbp->b_nwnd == 0) {
  52.                                 curbp->b_dotp  = curwp->w_dotp;
  53.                                 curbp->b_doto  = curwp->w_doto;
  54.                                 curbp->b_markp = curwp->w_markp;
  55.                                 curbp->b_marko = curwp->w_marko;
  56.                         }
  57.                         curbp = bp;
  58.                         curwp->w_bufp  = bp;
  59.                         if (bp->b_nwnd++ == 0) {
  60.                                 curwp->w_dotp  = bp->b_dotp;
  61.                                 curwp->w_doto  = bp->b_doto;
  62.                                 curwp->w_markp = bp->b_markp;
  63.                                 curwp->w_marko = bp->b_marko;
  64.                         } else {
  65.                                 wp = wheadp;
  66.                                 while (wp != NULL) {
  67.                                         if (wp!=curwp && wp->w_bufp==bp) {
  68.                                                 curwp->w_dotp  = wp->w_dotp;
  69.                                                 curwp->w_doto  = wp->w_doto;
  70.                                                 curwp->w_markp = wp->w_markp;
  71.                                                 curwp->w_marko = wp->w_marko;
  72.                                                 break;
  73.                                         }
  74.                                         wp = wp->w_wndp;
  75.                                 }
  76.                         }
  77.                         lp = curwp->w_dotp;
  78.                         i = curwp->w_ntrows/2;
  79.                         while (i-- && lback(lp)!=curbp->b_linep)
  80.                                 lp = lback(lp);
  81.                         curwp->w_linep = lp;
  82.                         curwp->w_flag |= WFMODE|WFHARD;
  83.                         mlwrite("[Old buffer]");
  84.                         return (TRUE);
  85.                 }
  86.         }
  87.         makename(bname, fname);                 /* New buffer name.     */
  88.         while ((bp=bfind(bname, FALSE, 0)) != NULL) {
  89.                 s = mlreply("Buffer name: ", bname, NBUFN);
  90.                 if (s == ABORT)                 /* ^G to just quit      */
  91.                         return (s);
  92.                 if (s == FALSE) {               /* CR to clobber it     */
  93.                         makename(bname, fname);
  94.                         break;
  95.                 }
  96.         }
  97.         if (bp==NULL && (bp=bfind(bname, TRUE, 0))==NULL) {
  98.                 mlwrite("Cannot create buffer");
  99.                 return (FALSE);
  100.         }
  101.         if (--curbp->b_nwnd == 0) {             /* Undisplay.           */
  102.                 curbp->b_dotp = curwp->w_dotp;
  103.                 curbp->b_doto = curwp->w_doto;
  104.                 curbp->b_markp = curwp->w_markp;
  105.                 curbp->b_marko = curwp->w_marko;
  106.         }
  107.         curbp = bp;                             /* Switch to it.        */
  108.         curwp->w_bufp = bp;
  109.         curbp->b_nwnd++;
  110.         return (readin(fname));                 /* Read it in.          */
  111. }
  112.  
  113. /*
  114.  * Read file "fname" into the current
  115.  * buffer, blowing away any text found there. Called
  116.  * by both the read and visit commands. Return the final
  117.  * status of the read. Also called by the mainline,
  118.  * to read in a file specified on the command line as
  119.  * an argument.
  120.  */
  121. readin(fname)
  122. char    fname[];
  123. {
  124.         register LINE   *lp1;
  125.         register LINE   *lp2;
  126.         register int    i;
  127.         register WINDOW *wp;
  128.         register BUFFER *bp;
  129.         register int    s;
  130.         register int    nbytes;
  131.         register int    nline;
  132.         char            line[NLINE];
  133.  
  134.         bp = curbp;                             /* Cheap.               */
  135.         if ((s=bclear(bp)) != TRUE)             /* Might be old.        */
  136.                 return (s);
  137.         bp->b_flag &= ~(BFTEMP|BFCHG);
  138.         strcpy(bp->b_fname, fname);
  139.         if ((s=ffropen(fname)) == FIOERR)       /* Hard file open.      */
  140.                 goto out;
  141.         if (s == FIOFNF) {                      /* File not found.      */
  142.                 mlwrite("[New file]");
  143.                 goto out;
  144.         }
  145.         mlwrite("[Reading file]");
  146.         nline = 0;
  147.         while ((s=ffgetline(line, NLINE)) == FIOSUC) {
  148.                 nbytes = strlen(line);
  149.                 if ((lp1=lalloc(nbytes)) == NULL) {
  150.                         s = FIOERR;             /* Keep message on the  */
  151.                         break;                  /* display.             */
  152.                 }
  153.                 lp2 = lback(curbp->b_linep);
  154.                 lp2->l_fp = lp1;
  155.                 lp1->l_fp = curbp->b_linep;
  156.                 lp1->l_bp = lp2;
  157.                 curbp->b_linep->l_bp = lp1;
  158.                 for (i=0; i<nbytes; ++i)
  159.                         lputc(lp1, i, line[i]);
  160.                 ++nline;
  161.         }
  162.         ffclose();                              /* Ignore errors.       */
  163.         if (s == FIOEOF) {                      /* Don't zap message!   */
  164.                 if (nline == 1)
  165.                         mlwrite("[Read 1 line]");
  166.                 else
  167.                         mlwrite("[Read %d lines]", nline);
  168.         }
  169. out:
  170.         for (wp=wheadp; wp!=NULL; wp=wp->w_wndp) {
  171.                 if (wp->w_bufp == curbp) {
  172.                         wp->w_linep = lforw(curbp->b_linep);
  173.                         wp->w_dotp  = lforw(curbp->b_linep);
  174.                         wp->w_doto  = 0;
  175.                         wp->w_markp = NULL;
  176.                         wp->w_marko = 0;
  177.                         wp->w_flag |= WFMODE|WFHARD;
  178.                 }
  179.         }
  180.         if (s == FIOERR)                        /* False if error.      */
  181.                 return (FALSE);
  182.         return (TRUE);
  183. }
  184.  
  185. /*
  186.  * Take a file name, and from it
  187.  * fabricate a buffer name. This routine knows
  188.  * about the syntax of file names on the target system.
  189.  * I suppose that this information could be put in
  190.  * a better place than a line of code.
  191.  */
  192. makename(bname, fname)
  193. char    bname[];
  194. char    fname[];
  195. {
  196.         register char   *cp1;
  197.         register char   *cp2;
  198.  
  199.         cp1 = &fname[0];
  200.         while (*cp1 != 0)
  201.                 ++cp1;
  202.  
  203. #if     AMIGA
  204.         while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='/')
  205.                 --cp1;
  206. #endif
  207. #if     VMS
  208.         while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!=']')
  209.                 --cp1;
  210. #endif
  211. #if     CPM
  212.         while (cp1!=&fname[0] && cp1[-1]!=':')
  213.                 --cp1;
  214. #endif
  215. #if     MSDOS
  216.         while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='\\')
  217.                 --cp1;
  218. #endif
  219. #if     V7
  220.         while (cp1!=&fname[0] && cp1[-1]!='/')
  221.                 --cp1;
  222. #endif
  223.         cp2 = &bname[0];
  224.         while (cp2!=&bname[NBUFN-1] && *cp1!=0 && *cp1!=';')
  225.                 *cp2++ = *cp1++;
  226.         *cp2 = 0;
  227. }
  228.  
  229. /*
  230.  * Ask for a file name, and write the
  231.  * contents of the current buffer to that file.
  232.  * Update the remembered file name and clear the
  233.  * buffer changed flag. This handling of file names
  234.  * is different from the earlier versions, and
  235.  * is more compatable with Gosling EMACS than
  236.  * with ITS EMACS. Bound to "C-X C-W".
  237.  */
  238. filewrite(f, n)
  239. {
  240.         register WINDOW *wp;
  241.         register int    s;
  242.         char            fname[NFILEN];
  243.  
  244.         if ((s=mlreply("Write file: ", fname, NFILEN)) != TRUE)
  245.                 return (s);
  246.         if ((s=writeout(fname)) == TRUE) {
  247.                 strcpy(curbp->b_fname, fname);
  248.                 curbp->b_flag &= ~BFCHG;
  249.                 wp = wheadp;                    /* Update mode lines.   */
  250.                 while (wp != NULL) {
  251.                         if (wp->w_bufp == curbp)
  252.                                 wp->w_flag |= WFMODE;
  253.                         wp = wp->w_wndp;
  254.                 }
  255.         }
  256.         return (s);
  257. }
  258.  
  259. /*
  260.  * Save the contents of the current
  261.  * buffer in its associatd file. No nothing
  262.  * if nothing has changed (this may be a bug, not a
  263.  * feature). Error if there is no remembered file
  264.  * name for the buffer. Bound to "C-X C-S". May
  265.  * get called by "C-Z".
  266.  */
  267. filesave(f, n)
  268. {
  269.         register WINDOW *wp;
  270.         register int    s;
  271.  
  272.         if ((curbp->b_flag&BFCHG) == 0)         /* Return, no changes.  */
  273.                 return (TRUE);
  274.         if (curbp->b_fname[0] == 0) {           /* Must have a name.    */
  275.                 mlwrite("No file name");
  276.                 return (FALSE);
  277.         }
  278.         if ((s=writeout(curbp->b_fname)) == TRUE) {
  279.                 curbp->b_flag &= ~BFCHG;
  280.                 wp = wheadp;                    /* Update mode lines.   */
  281.                 while (wp != NULL) {
  282.                         if (wp->w_bufp == curbp)
  283.                                 wp->w_flag |= WFMODE;
  284.                         wp = wp->w_wndp;
  285.                 }
  286.         }
  287.         return (s);
  288. }
  289.  
  290. /*
  291.  * This function performs the details of file
  292.  * writing. Uses the file management routines in the
  293.  * "fileio.c" package. The number of lines written is
  294.  * displayed. Sadly, it looks inside a LINE; provide
  295.  * a macro for this. Most of the grief is error
  296.  * checking of some sort.
  297.  */
  298. writeout(fn)
  299. char    *fn;
  300. {
  301.         register int    s;
  302.         register LINE   *lp;
  303.         register int    nline;
  304.  
  305.         if ((s=ffwopen(fn)) != FIOSUC)          /* Open writes message. */
  306.                 return (FALSE);
  307.         lp = lforw(curbp->b_linep);             /* First line.          */
  308.         nline = 0;                              /* Number of lines.     */
  309.         while (lp != curbp->b_linep) {
  310.                 if ((s=ffputline(&lp->l_text[0], llength(lp))) != FIOSUC)
  311.                         break;
  312.                 ++nline;
  313.                 lp = lforw(lp);
  314.         }
  315.         if (s == FIOSUC) {                      /* No write error.      */
  316.                 s = ffclose();
  317.                 if (s == FIOSUC) {              /* No close error.      */
  318.                         if (nline == 1)
  319.                                 mlwrite("[Wrote 1 line]");
  320.                         else
  321.                                 mlwrite("[Wrote %d lines]", nline);
  322.                 }
  323.         } else                                  /* Ignore close error   */
  324.                 ffclose();                      /* if a write error.    */
  325.         if (s != FIOSUC)                        /* Some sort of error.  */
  326.                 return (FALSE);
  327.         return (TRUE);
  328. }
  329.  
  330. /*
  331.  * The command allows the user
  332.  * to modify the file name associated with
  333.  * the current buffer. It is like the "f" command
  334.  * in UNIX "ed". The operation is simple; just zap
  335.  * the name in the BUFFER structure, and mark the windows
  336.  * as needing an update. You can type a blank line at the
  337.  * prompt if you wish.
  338.  */
  339. filename(f, n)
  340. {
  341.         register WINDOW *wp;
  342.         register int    s;
  343.         char            fname[NFILEN];
  344.  
  345.         if ((s=mlreply("Name: ", fname, NFILEN)) == ABORT)
  346.                 return (s);
  347.         if (s == FALSE)
  348.                 strcpy(curbp->b_fname, "");
  349.         else
  350.                 strcpy(curbp->b_fname, fname);
  351.         wp = wheadp;                            /* Update mode lines.   */
  352.         while (wp != NULL) {
  353.                 if (wp->w_bufp == curbp)
  354.                         wp->w_flag |= WFMODE;
  355.                 wp = wp->w_wndp;
  356.         }
  357.         return (TRUE);
  358. }
  359.  
  360.